package software.lib.action;

import software.lib.entity.Admin;
import software.lib.entity.Person;
import software.lib.entity.User;
import software.lib.exception.NoSuchEntityException;
import software.lib.service.UserService;
import software.lib.util.IgnoreFieldProcessorImpl;
import net.sf.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Wang on 2017/5/4.
 */
@Controller
public class UserAction {

    @RequestMapping(value = "/login",produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
    public @ResponseBody
    Object login(@ModelAttribute User user, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            User u = userService.verify(user);
            if(u!=null){
                session.setAttribute("user",u);
                jsonObject.accumulate("user",u, IgnoreFieldProcessorImpl.getDefaultConfig());
                jsonObject.put("status",true);
            }else {
                jsonObject.put("message","密码错误！");
                jsonObject.put("status",false);
            }
        }catch (NoSuchEntityException |IllegalArgumentException e){
            jsonObject.put("message",e.getMessage());
            jsonObject.put("status",false);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/loginCheck",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    public @ResponseBody
    Object checkLogin(HttpSession session){
        JSONObject jsonObject = new JSONObject();
        User u = (User) session.getAttribute("user");
        if(u==null){
            jsonObject.put("status",false);
        }else {
            jsonObject.put("type",u.getClass().getName());
            jsonObject.put("status",true);
        }
        jsonObject.accumulate("user",u,IgnoreFieldProcessorImpl.getDefaultConfig());
        return jsonObject;
    }

    @RequestMapping(value = "/loginOut",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    public @ResponseBody
    Object loginOut(HttpSession session){
        JSONObject jsonObject = new JSONObject();
        User u = (User) session.getAttribute("user");
        if(u!=null){
            session.removeAttribute("user");
            jsonObject.put("message","登出成功！");
        }
        return jsonObject;
    }

    @RequestMapping(value = "/user/delete",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    public @ResponseBody
    Object userDelete(User user){
        JSONObject jsonObject = new JSONObject();
        try{
            boolean status = userService.deleteUser(user);
            jsonObject.put("status",status);
        }catch (NoSuchEntityException|IllegalArgumentException e){
            jsonObject.put("message",e.getMessage());
            jsonObject.put("status",false);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/user/update",produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
    public @ResponseBody
    Object userUpdate(@ModelAttribute User user,HttpSession session){
        JSONObject jsonObject = new JSONObject();
        User u = (User) session.getAttribute("user");
        if(u==null){
            jsonObject.put("status",false);
        }else {
            BeanUtils.copyProperties(user,u,"idCard","useful");
            userService.modifyUser(u);
            jsonObject.put("status",true);
        }
        jsonObject.accumulate("user",u,IgnoreFieldProcessorImpl.getDefaultConfig());
        return jsonObject;
    }

    @RequestMapping(value = "/regist",produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
    public @ResponseBody
    Object regist(@ModelAttribute Person user, HttpSession session){
        return regist(user);
    }

    private JSONObject regist(@ModelAttribute User user) {
        JSONObject jsonObject = new JSONObject();
        try{
            User u = userService.regist(user);
            jsonObject.put("user",u);
            jsonObject.put("status",true);
        }catch (DuplicateKeyException |IllegalArgumentException e){
            jsonObject.put("message",e.getMessage());
            jsonObject.put("status",false);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/registCleaner",produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
    public @ResponseBody
    Object registEditor(@ModelAttribute Admin user, HttpSession session){
        return regist(user);
    }

    @Autowired
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
